package com.hnevc.wechatdemo;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    //创建Fragment的实例
    FindFragment findFragment;
    MyFragment myFragment;
    WeChatFragment weChatFragment;
    ContactsFragment contactsFragment;
    //创建一个Fragment列表
    List<Fragment> fragmentList = new ArrayList<>();
    private ViewPager viewPager;
    private FragmentPagerAdapter adapter;

    LinearLayout mTabWeChat,mTabContacts,mTabFind,mTabMy;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findFragment = new FindFragment();
        myFragment = new MyFragment();
        weChatFragment = new WeChatFragment();
        contactsFragment = new ContactsFragment();
        //将Fragment添加到list中
        fragmentList.add(weChatFragment);
        fragmentList.add(contactsFragment);
        fragmentList.add(findFragment);
        fragmentList.add(myFragment);

        adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {

                return fragmentList.get(position);
            }

            @Override
            public int getCount() {
                return fragmentList.size();
            }
        };

        viewPager = (ViewPager) findViewById(R.id.id_container);
        viewPager.setAdapter(adapter);

        mTabWeChat = (LinearLayout) findViewById(R.id.id_wechat);
        mTabContacts = (LinearLayout) findViewById(R.id.id_contacts);
        mTabFind = (LinearLayout) findViewById(R.id.id_find);
        mTabMy = (LinearLayout) findViewById(R.id.id_my);

        viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
            @Override
            public void onPageSelected(int position) {
                switch (position){
                    case 0:
                        mTabWeChat.setSelected(true);
                        mTabContacts.setSelected(false);
                        mTabFind.setSelected(false);
                        mTabMy.setSelected(false);
                        break;
                    case 1:
                        mTabWeChat.setSelected(false);
                        mTabContacts.setSelected(true);
                        mTabFind.setSelected(false);
                        mTabMy.setSelected(false);
                        break;
                    case 2:
                        mTabWeChat.setSelected(false);
                        mTabContacts.setSelected(false);
                        mTabFind.setSelected(true);
                        mTabMy.setSelected(false);
                        break;
                    case 3:
                        mTabWeChat.setSelected(false);
                        mTabContacts.setSelected(false);
                        mTabFind.setSelected(false);
                        mTabMy.setSelected(true);
                        break;
                }
            }
        });


    }



    public void buttonClick(View view) {
        //切换不同的Fragment
        int id=view.getId();//获取点击事件的触发控件的id
        switch (id){
            case R.id.id_wechat:{
               /* //获取FragmentManager，通过getSupportFragmentManager（）获取。
                FragmentManager fm = getSupportFragmentManager();
                //开启事务，调用beginTransaction（）开启
                FragmentTransaction transaction = fm.beginTransaction();
                //向容器中添加（add）或替换（replace）Fragment；需要容器id和Fragment实例
                transaction.replace(R.id.id_container,weChatFragment);
                //提交事务
                transaction.commit();
*/
               viewPager.setCurrentItem(0);
                break;
            }
            case R.id.id_contacts:{
               /* //获取FragmentManager，通过getSupportFragmentManager（）获取。
                FragmentManager fm = getSupportFragmentManager();
                //开启事务，调用beginTransaction（）开启
                FragmentTransaction transaction = fm.beginTransaction();
                //向容器中添加（add）或替换（replace）Fragment；需要容器id和Fragment实例
                transaction.replace(R.id.id_container,contactsFragment);
                //提交事务
                transaction.commit();*/
                viewPager.setCurrentItem(1);
                break;
            }
            case R.id.id_find:{
                /*//获取FragmentManager，通过getSupportFragmentManager（）获取。
                FragmentManager fm = getSupportFragmentManager();
                //开启事务，调用beginTransaction（）开启
                FragmentTransaction transaction = fm.beginTransaction();
                //向容器中添加（add）或替换（replace）Fragment；需要容器id和Fragment实例
                transaction.replace(R.id.id_container,findFragment);
                //提交事务
                transaction.commit();*/
                viewPager.setCurrentItem(2);
                break;
            }
            case R.id.id_my:{
               /* //获取FragmentManager，通过getSupportFragmentManager（）获取。
                FragmentManager fm = getSupportFragmentManager();
                //开启事务，调用beginTransaction（）开启
                FragmentTransaction transaction = fm.beginTransaction();
                //向容器中添加（add）或替换（replace）Fragment；需要容器id和Fragment实例
                transaction.replace(R.id.id_container,myFragment);
                //提交事务
                transaction.commit();*/
                viewPager.setCurrentItem(3);
                break;
            }
        }
    }
}
